package judesart.app.client.ui.admin.profile;

import judesart.app.client.controller.ProfileController;
import judesart.app.client.controller.ProfileControllerAsync;
import judesart.app.client.widget.TopInfo;
import judesart.app.domain.Artist;
import judesart.app.domain.Profile;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.InfoConfig;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HtmlEditor;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.appengine.api.datastore.Text;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class ProfilePanel extends LayoutContainer
{
    private final class SaveArtistCallback implements AsyncCallback<Artist>
    {
        @Override
        public void onSuccess(Artist result)
        {
            TopInfo.display("Save Ok", "Profile information saved.");
        }

        @Override
        public void onFailure(Throwable caught)
        {
            InfoConfig ic = new InfoConfig("Save Error",
                    "Unable to save profile information...");
            ic.display = 5000;
            TopInfo.display(ic);
        }
    }

    ProfileControllerAsync profileController = GWT.create(ProfileController.class);
    Artist artist;
    boolean isNew;
    private final FormPanel frmpnlProfile;
    private final Button btnSave;
    private final TextField<String> titleField;
    private final HtmlEditor detailHtmlEditor;

    public ProfilePanel()
    {
        setSize("800", "600");

        frmpnlProfile = new FormPanel();
        frmpnlProfile.setFrame(true);
        frmpnlProfile.setLabelSeparator("");
        frmpnlProfile.setLabelAlign(LabelAlign.TOP);
        frmpnlProfile.setButtonAlign(HorizontalAlignment.CENTER);
        frmpnlProfile.setHeading("Profile");

        titleField = new TextField<String>();
        frmpnlProfile.add(titleField, new FormData("100%"));
        titleField.setFieldLabel("Title");
        titleField.setName("title");

        detailHtmlEditor = new HtmlEditor();
        frmpnlProfile.add(detailHtmlEditor, new FormData("100%"));
        detailHtmlEditor.setFieldLabel("Detail");
        detailHtmlEditor.setName("detail");

        btnSave = new Button("Save");
        frmpnlProfile.addButton(btnSave);

        Button btnCancel = new Button("Cancel");
        frmpnlProfile.addButton(btnCancel);
        add(frmpnlProfile);

        btnSave.addListener(Events.Select, new Listener<ButtonEvent>()
        {
            public void handleEvent(ButtonEvent e)
            {
                save();
            }
        });
        btnCancel.addListener(Events.Select, new Listener<ButtonEvent>()
        {
            public void handleEvent(ButtonEvent e)
            {
                refresh();
            }
        });

        refresh();
    }

    private void refresh()
    {
        profileController.getArtist(new AsyncCallback<Artist>()
        {

            @Override
            public void onSuccess(Artist result)
            {
                if (result == null)
                {
                    result = new Artist();
                    Profile profile = new Profile();
                    result.setProfile(profile);
                    setIsNew(true);
                }
                setArtist(result);
            }

            @Override
            public void onFailure(Throwable caught)
            {
                TopInfo.display("Error", "Unable to get Artist information");
            }
        });
    }

    private void save()
    {
        bindToModel();
        if (isNew)
        {
            profileController.createArtist(artist, new SaveArtistCallback());
        } else
        {
            profileController.updateArtist(artist, new SaveArtistCallback());
        }
    }

    private void setIsNew(boolean isNew)
    {
        this.isNew = isNew;
    }

    private void setArtist(Artist artist)
    {
        this.artist = artist;
        bindToForm();
    }

    private void bindToModel()
    {
        artist.getProfile().setTitle(titleField.getValue());
        artist.getProfile().setDetail(new Text(detailHtmlEditor.getValue()));
    }

    private void bindToForm()
    {
        titleField.setValue(artist.getProfile().getTitle());
        detailHtmlEditor.setValue(artist.getProfile().getDetail().getValue());
    }
}
